package com.zsj.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * 实现接口(ApplicationContextAware)的setApplicationContext(ApplicationContext context)方法，
 * 并保存ApplicationContext 对象。Spring初始化时，自动会通过该方法将ApplicationContext对象注入。
 * 之后在Spring容器所管理的类中，之间使用 ApplicationContextUtil类获取spring所管理的bean对象。
 */
public class ApplicationContextUtil  implements  ApplicationContextAware{

	private static ApplicationContext applicationContext;

	public  void setApplicationContext(ApplicationContext context) {
		ApplicationContextUtil.applicationContext = context;
	}

	/* 获取ApplicationContext对象 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/* 从Spring IoC容器中获取spring所管理的bean对象 */
	public static Object getBean(String beanId) {
		return applicationContext.getBean(beanId);
	}
	
}
